package com.dollarxy.dlmall.data.scheduled;

import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.dollarxy.dlmall.data.component.UmengWxMiniComponent;
import com.dollarxy.dlmall.data.dao.OverviewDao;
import com.dollarxy.dlmall.data.entity.OverviewEntity;
import com.dollarxy.dlmall.data.entity.TotalUserEntity;
import com.dollarxy.dlmall.data.service.OverviewService;
import com.dollarxy.dlmall.data.service.TotalUserService;
import com.dollarxy.dlmall.data.util.DateUtils;
import com.umeng.umini.param.UmengUminiAppIndicatorDTO;
import com.umeng.umini.param.UmengUminiGetOverviewResult;
import com.umeng.umini.param.UmengUminiGetTotalUserResult;
import com.umeng.umini.param.UmnegUminiTotalUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class UmengScheduled1d {
    @Autowired
    UmengWxMiniComponent umengWxMiniComponent;


    @Autowired
    OverviewService overviewService;
    @Autowired
    TotalUserService totalUserService;
    @Autowired
    OverviewDao overviewDao ;

    String beginTime;
    String endTime;


    @Scheduled(cron = "1 3 5 5 5 * ")
    public void getUmengData() {


        ApiExecutor apiExecutor = new ApiExecutor("8537907", "UZfST6nSjl");
        apiExecutor.setServerHost("gateway.open.umeng.com");
        beginTime = DateUtils.format(new Date(),DateUtils.DATE_PATTERN);
        endTime = DateUtils.format(new Date(),DateUtils.DATE_PATTERN);



        try {
            //Overview data
            UmengWxMiniComponent umengWxMiniComponent = new UmengWxMiniComponent();
            UmengUminiGetOverviewResult result = umengWxMiniComponent.umengUminiGetOverview(apiExecutor,beginTime,endTime,"day","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");

            UmengUminiAppIndicatorDTO umengUminiAppIndicatorDTO = result.getData().getData()[0];
            OverviewEntity overviewEntity = new OverviewEntity();
            BeanUtils.copyProperties(umengUminiAppIndicatorDTO, overviewEntity);
            overviewEntity.setType(3);
            overviewService.save(overviewEntity);
            Thread.sleep(50000);

            UmengUminiGetTotalUserResult resultTotalUserResult = umengWxMiniComponent.umengUminiGetTotalUser(apiExecutor,beginTime,endTime,"day","newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");
            UmnegUminiTotalUserDTO umnegUminiTotalUserDTO = resultTotalUserResult.getData()[0];
            TotalUserEntity totalUserEntity = new TotalUserEntity();
            BeanUtils.copyProperties(umnegUminiTotalUserDTO, totalUserEntity);
            totalUserEntity.setType(1);
            totalUserService.save(totalUserEntity);
            Thread.sleep(50000);





        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            lock.unlock();
        }
    }
}
